<?php

function test()
{
    echo "Hello world";
}


/**
 * 递归菜单列表
 * 递归就是找儿子，区别是每次找儿子的爹不同
 * @param $data
 * @param int $pid
 * @return array
 */
function array_to_tree($data, $pid = 0)
{
    $menuList = [];
    foreach ($data as $key => $value) {
        //找到自己的儿子
        if ($value['pid'] == $pid) {
            //在大容器中记录第一个爹找的儿子
            $menuList[$value['id']] = $value;
            //排除自己找儿子的儿子
            unset($data[$key]);
            //继续查找儿子的儿子
            $menuList[$value['id']]['children'] = array_to_tree($data, $value['id']);
        }
    }
    return $menuList;
}

/**
 * 引用菜单列表
 * @param $data
 * @return array
 */
function data_to_tree($data)
{
    $tree = [];
    $newData = [];
    //循环重新排列
    foreach ($data as $datum) {
        $newData[$datum['id']] = $datum;
    }
    foreach ($newData as $key => $datum) {
        if ($datum['pid'] > 0) {
            //不是根节点的将自己的地址放到父级的child节点
            $newData[$datum['pid']]['children'][] = &$newData[$key];
        } else {
            //根节点直接把地址放到新数组中
            $tree[] = &$newData[$datum['id']];
        }
    }
    return $tree;
}
